package com.aaa.dang.modules.user.service.impl;

import com.aaa.dang.common.entity.Area;
import com.aaa.dang.common.entity.User;
import com.aaa.dang.common.systemconst.ISystemConst;
import com.aaa.dang.common.systemlog.annotation.SysLog;
import com.aaa.dang.common.util.BaseContrllor;
import com.aaa.dang.common.util.ResultModel;
import com.aaa.dang.modules.build.dao.IAreaDao;
import com.aaa.dang.modules.build.dao.IBuildDao;
import com.aaa.dang.modules.review.dao.IReviewDao;
import com.aaa.dang.modules.review.service.IReviewService;
import com.aaa.dang.modules.user.dao.IRoomChangeDao;
import com.aaa.dang.modules.user.dao.IUserDao;
import com.aaa.dang.modules.user.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.mysql.cj.x.protobuf.MysqlxCrud;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author JinChaoLiang
 * @calss QY87
 * @description 描述
 * @date 2018/12/14
 */
@Service
public class UserServiceImpl extends BaseContrllor implements IUserService {

    @Autowired
    private IUserDao userDao;

    @Autowired
    private ResultModel model;

    @Autowired
    private HttpSession session;

    @Autowired
    private IAreaDao areaDao;

    @Autowired
    private IBuildDao buildDao;



    /**
     * 事务注解
     * @param query
     * @return
     */
    @Transactional
    @Override
    public ResultModel getList(Map<String,Object> query) {
        int page = Integer.parseInt(query.get("page")+"");
        int limit = Integer.parseInt(query.get("limit")+"");
        PageHelper.startPage(page,limit);
        List<Map<String,Object>> list = userDao.getList(query);
        int c = userDao.getCount(query);
        ResultModel rm = new ResultModel();
        rm.setCode(ISystemConst.SUCCESS_CODE);
        rm.setCount(c);
        rm.setData(list);
        return rm;
    }

    /**
     * 用于首页得到所有入住人数
     * @return
     */
    @Override
    public Integer getCount() {
        return userDao.getCountInfo();
    }

    @Override
    public Map<String,Object> getInfoById(int id) {
        Map<String,Object> map = userDao.getInfoById(id);
        return map;
    }

    /**
     * 获取用户信息
     * @param username
     * @param psw
     * @return
     */

    @Override
    public ResultModel login(String username, String psw) {

        int count = userDao.login(username,psw);
        model.setCode(ISystemConst.FAULT_CODE);
        if(count== ISystemConst.FAULT_CODE){
            model.setCode(ISystemConst.SUCCESS_CODE);
            model=returnSuccessInfo("账号密码正确");
            User user =userDao.getUser(username);
            model.setData(user);
            System.out.println(model);

        }else{
            model=returnErrorInfo("账号或密码错误");
        }
        return model;
    }

    /***
     * 手机号唯一性校验
     * @param username
     * @return
     */
    @Override
    public ResultModel checkPhone(String username) {
        model.setData(null);
        model.setCode(ISystemConst.FAULT_CODE);
        try {
            int count = userDao.checkPhone(username);
            if(count==ISystemConst.SUCCESS_CODE) {
                model.setMsg("手机号可以使用");
                model.setCode(ISystemConst.SUCCESS_CODE);
            } else {
                model.setMsg("该手机号已存在");
            }
        } catch (Exception e) {
            model.setMsg("数据校验异常");
            e.printStackTrace();
        }


        return model;
    }

    /***
     * 注册用户
     * @param username
     * @param password
     * @return
     */
    @Override
    public ResultModel addUser(String username, String password) {
        model.setData(null);
        model.setCode(ISystemConst.FAULT_CODE);
        try {
            int count = userDao.checkPhone(username);
            if (count == ISystemConst.SUCCESS_CODE) {
                try {
                    int rows = userDao.addUser(username, password);
                    if (rows > ISystemConst.SUCCESS_CODE) {
                        model.setCode(ISystemConst.SUCCESS_CODE);
                        model.setMsg("注册成功");
                    } else {
                        model.setMsg("添加失败");
                    }
                } catch (Exception e) {
                    model.setMsg("添加异常");
                    e.printStackTrace();
                }
            } else {
                model.setMsg("手机号已存在，请直接登录");
            }
        } catch (Exception e) {
            model.setMsg("查询数据异常");
            e.printStackTrace();
        }
        return model;
    }

    /**
     * 手机端修改密码
     * @param map
     * @return
     */
    @Override

    public ResultModel updatepsw(Map<String, Object> map) {
        ResultModel rm = new ResultModel();
        userDao.updatepsw(map);
        rm.setCode(ISystemConst.SUCCESS_CODE);
        rm.setData("");
        return rm;
    }

    /**
     * 手机端完善资料
     * @param map
     * @return
     */
    @Transactional
    @Override
    public ResultModel updateinfo(Map<String, Object> map) {
        ResultModel rm = new ResultModel();
        userDao.updateinfo(map);
        rm.setCode(ISystemConst.SUCCESS_CODE);
        rm.setData("");
        return rm;
    }

    /**
     * 手机端得到部分信息
     * @return
     */
    @Override
    public ResultModel getUserPartialInfo(String username) {
        ResultModel rm = new ResultModel();
        Map<String, Object> map = userDao.getUserPartialInfo(username);
        if (map == null) {
            rm.setCode(ISystemConst.FAULT_CODE);
        } else {
            rm.setCode(ISystemConst.SUCCESS_CODE);
            rm.setData(map);
        }
        return rm;
    }
    /**
     * 找回密码之更新密码
     * @param query
     * @return
     */

    @Override
    public ResultModel findpassword(Map<String, Object> query) {
        userDao.updatepsw(query);
        model=returnSuccessInfo("更新成功");
        return model;

    }

    /**
     * 获得所有小区
     * @return
     */
    @Override
    public ResultModel getAllArea() {
        model.setCode(ISystemConst.FAULT_CODE);
        try {
            List<Area> data = areaDao.getAllArea();
            model.setCode(ISystemConst.SUCCESS_CODE);
            model.setData(data);
            model.setMsg("查询成功");
        } catch (Exception e) {
            model.setMsg("查询异常");
            model.setData(null);
            e.printStackTrace();
        }
        return model;
    }

    /***
     * 根据小区id获得楼栋
     * @param id
     * @return
     */
    @Override
    public ResultModel getBuildingsByAreaId(Integer id) {
        model.setData(null);
        model.setCode(ISystemConst.FAULT_CODE);
        try {
            List<Map<String, Object>> data = areaDao.getBuildingsByAreaId(id);
            model.setData(data);
            model.setCode(ISystemConst.SUCCESS_CODE);
            model.setMsg("查询成功");
        } catch (Exception e) {
            model.setMsg("查询异常");
            e.printStackTrace();
        } finally {
            return model;
        }
    }

    /***
     * 根据楼栋id获得所有房间
     * @param id
     * @return
     */
    @Override
    public ResultModel getRoomsByBuildingId(Integer id) {
        model.setCode(ISystemConst.FAULT_CODE);
        try {
            List<Map<String, Object>> data = userDao.getRoomsByBuildingId(id);
            model.setCode(ISystemConst.SUCCESS_CODE);
            model.setData(data);
            if(data.size() > ISystemConst.SUCCESS_CODE) {
                model.setMsg("查询成功");
            } else {
                model.setMsg("该楼栋暂无房间");
            }

        } catch (Exception e) {
            model.setData(null);
            model.setMsg("查询异常");
            e.printStackTrace();
        }
        return model;
    }

    /***
     * 根据条件获得所有空闲房间
     * @param data
     * @return
     */
    @Override
    public ResultModel getRoomsByQuery(Map<String, Object> data) {
        model.setData(null);
        model.setCode(ISystemConst.FAULT_CODE);
        try {
            List<Map<String,Object>> list = userDao.getRoomsByQuery(data);
            model.setCode(ISystemConst.SUCCESS_CODE);
            model.setData(list);
            if(list.size() == ISystemConst.SUCCESS_CODE) {
                model.setMsg("暂无该条件房间");
            } else {
                model.setMsg("查询成功");
            }
        } catch (Exception e) {
            model.setMsg("查询异常");
            e.printStackTrace();
        }finally {
            return model;
        }
    }

    /***
     * 添加报修数据
     * @param data
     * @return
     */
    @Override
    public ResultModel addRepairapply(Map<String, Object> data) {
        model.setData(null);
        model.setCode(ISystemConst.FAULT_CODE);
        data.put("repairTime",new Date());
        try {
            int rows = userDao.addRepairapply(data);
            model.setCode(ISystemConst.SUCCESS_CODE);
            model.setMsg("添加成功");
        } catch (Exception e) {
            model.setMsg("添加失败");
            e.printStackTrace();
        } finally {
            return model;
        }
    }

    /***
     * 获得用户申请数据
     * @param userId
     * @return
     */
    @Override
    public ResultModel getRepairApplyByUserId(Integer userId) {
        model.setData(null);
        model.setCode(ISystemConst.FAULT_CODE);
        try {
            List<Map<String,Object>> data = userDao.getRepairApplyByUserId(userId);
            model.setCode(ISystemConst.SUCCESS_CODE);
            model.setData(data);
            if(data.size() == ISystemConst.SUCCESS_CODE) {
                model.setMsg("暂无报修数据");
            } else{
                model.setMsg("查询成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return model;
        }

    }

    /***
     * 修改用户订单的状态
     * @param data
     * @return
     */
    @Override
    public ResultModel updateStatusById(Map<String, Object> data) {
        model.setCode(ISystemConst.FAULT_CODE);
        model.setData(null);
        try {
            int rows = userDao.updateStatusById(data);
            model.setCode(ISystemConst.SUCCESS_CODE);
            model.setMsg("撤回成功");
        } catch (Exception e) {
            model.setMsg("撤回失败");
            e.printStackTrace();
        } finally {
            return model;
        }
    }

    /***
     * 根据用户id获得改用户的入住申请
     * @param userId
     * @return
     */
    @Override
    public ResultModel getStayApplyByUserId(Integer userId) {
        model.setData(null);
        model.setCode(ISystemConst.FAULT_CODE);
        try {
            List<Map<String,Object>> data = userDao.getStayApplyByUserId(userId);
            model.setCode(ISystemConst.SUCCESS_CODE);
            model.setData(data);
            if(data.size() > ISystemConst.SUCCESS_CODE) {
                model.setMsg("暂无您的申请记录");
            }
            model.setMsg("查询成功");
        } catch (Exception e) {
            model.setMsg("数据查询异常");
            e.printStackTrace();
        } finally {
            return model;
        }
    }

    /***
     * 撤回入住申请
     * @param data
     * @return
     */
    @Override
    public ResultModel updateStayApplyStatus(Map<String, Object> data) {
        model.setData(null);
        try {
            int rows = userDao.updateStayApplyStatus(data);
            model.setCode(ISystemConst.SUCCESS_CODE);
            model.setMsg("撤回成功");
        } catch (Exception e) {
            model.setCode(ISystemConst.FAULT_CODE);
            model.setMsg("撤回失败");
            e.printStackTrace();
        } finally {
            return model;
        }
    }

    /***
     * 根据用户id获得用户的退宿申请记录
     * @param userId
     * @return
     */
    @Override
    public ResultModel getRetireApply(Integer userId) {
        model.setCode(ISystemConst.FAULT_CODE);
        try {
            List<Map<String,Object>> data = userDao.getRetireApply(userId);
            model.setCode(ISystemConst.SUCCESS_CODE);
            if(data.size() == ISystemConst.SUCCESS_CODE) {
                model.setMsg("暂无退宿记录");
            } else {
                model.setData(data);
                model.setMsg("查询成功");
            }

        } catch (Exception e) {
            model.setMsg("查询失败");
            e.printStackTrace();
        } finally {
            return model;
        }
    }

    /***
     * 用户退宿撤回
     * @param data
     * @return
     */
    @Override
    public ResultModel updateRetireApplyStatus(Map<String, Object> data) {
        model.setData(null);
        try {
            int rows = userDao.updateRetireApplyStatus(data);
            model.setCode(ISystemConst.SUCCESS_CODE);
            model.setMsg("撤回成功");
        } catch (Exception e) {
            model.setCode(ISystemConst.FAULT_CODE);
            model.setMsg("撤回失败");
            e.printStackTrace();
        } finally {
            return model;
        }
    }

    /***
     * 根据用户id获得该用户的申请交押金记录
     * @param userId
     * @return
     */
    @Override
    public ResultModel getDepositApply(Integer userId) {
        model.setCode(ISystemConst.FAULT_CODE);
        try {
            List<Map<String,Object>> data = userDao.getDepositApply(userId);
            model.setCode(ISystemConst.SUCCESS_CODE);
            if(data.size() == ISystemConst.SUCCESS_CODE) {
                model.setMsg("暂无交押金记录");
            } else {
                model.setData(data);
                model.setMsg("查询成功");
            }

        } catch (Exception e) {
            model.setMsg("查询失败");
            e.printStackTrace();
        } finally {
            return model;
        }
    }

    /***
     * 撤回押金申请
     * @param map
     * @return
     */
    @Override
    public ResultModel updateDepositApplyStatus(Map<String, Object> map) {
        model.setData(null);
        try {
            int rows = userDao.updateDepositApplyStatus(map);
            model.setCode(ISystemConst.SUCCESS_CODE);
            model.setMsg("撤回成功");
        } catch (Exception e) {
            model.setCode(ISystemConst.FAULT_CODE);
            model.setMsg("撤回失败");
            e.printStackTrace();
        } finally {
            return model;
        }
    }
    /***
     * 根据用户id获得该用户的申请交租金记录
     * @param userId
     * @return
     */
    @Override
    public ResultModel getPayRentApply(Integer userId) {
        model.setCode(ISystemConst.FAULT_CODE);
        try {
            List<Map<String,Object>> data = userDao.getPayRentApply(userId);
            model.setCode(ISystemConst.SUCCESS_CODE);
            if(data.size() == ISystemConst.SUCCESS_CODE) {
                model.setMsg("暂无交租金记录");
            } else {
                model.setData(data);
                model.setMsg("查询成功");
            }

        } catch (Exception e) {
            model.setMsg("查询失败");
            e.printStackTrace();
        } finally {
            return model;
        }
    }

    /***
     * 撤回租金申请
     * @param map
     * @return
     */
    @Override
    public ResultModel updatePayRentApplyStatus(Map<String, Object> map) {
        model.setData(null);
        try {
            int rows = userDao.updatePayRentApplyStatus(map);
            model.setCode(ISystemConst.SUCCESS_CODE);
            model.setMsg("撤回成功");
        } catch (Exception e) {
            model.setCode(ISystemConst.FAULT_CODE);
            model.setMsg("撤回失败");
            e.printStackTrace();
        } finally {
            return model;
        }
    }

    @Override
    public ResultModel getUser(int id) {
        User userById = userDao.getUserById(id);
        model.setData(userById);
        model.setCode(ISystemConst.SUCCESS_CODE);
        return model;
    }
}
